package mpbe.Componentes.TableColEdit;

/*
 * Este codigo es software libre y se encuentra licenciado 
 * bajo los términos de la Licencia Pública General de GNU versión 3 
 * según se encuentra publicada por la Free Software Foundation.
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Autor: Martin Pablo Bellanca <martin.bellanca@gmail.com>
 * web: http://mpbe.blogspot.com/
 */

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ControlEditor;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

public class WgComboCE extends WidgetsTCE {
	private Combo wgEditor = null;
	private ModifyCellListener modifyListener = null;
	private SelectionListener selectionListener = null;
	
	private EditingTCE editandoT;
	private TableCursor cursorT;
	private ControlEditor editorT;
	private Table tableT;
	
	public WgComboCE(int SWT, String[] lista, String txtdefault) {
		super(SWT, lista, txtdefault);
	}

	public WgComboCE(int SWT) {
		super(SWT);
	}

	public WgComboCE(int SWT, String[] lista) {
		super(SWT, lista);
	}

	public WgComboCE(int SWT, String txtdefault) {
		super(SWT, txtdefault);
	}

	@Override
	public Control getWidget() {
		return wgEditor;
	}

	@Override
	public int indexOfList(String TXT) {
		int ii = -1, jj=0;
		do {
			if (super.TXTList[jj]==TXT) ii=jj;
			jj++;
		} while ((ii<0) && (jj<super.TXTList.length));
		return ii;
	}

	@Override
	public void onCursoKeyChar(EditingTCE editando, TableCursor cursor,
			ControlEditor editor, Table table, char car) {
		System.out.println("cursor key caracter "+car);
		int ii=0;
		while (ii<TXTList.length){
			String txt = ""+car;
			if ((TXTList[ii].startsWith(txt.toLowerCase())==true) || (TXTList[ii].startsWith(txt.toUpperCase())==true)) {			
				editandoT = editando;
				cursorT = cursor;
				editorT = editor;
				tableT = table;
				editando.startEditing(cursor.getRow(), cursor.getColumn());
				editar(ii);
				ii=TXTList.length;
			}
			ii++;
		}
		
		//System.out.println("cursor key caracter");
		// buscar si la letra coincide con la primera letra de la lista, seleccionar y editar
	}

	@Override
	public void onCursorKeyF2(EditingTCE editando, TableCursor cursor,
			ControlEditor editor, Table table) {
		//	System.out.println("cursor key F2");
		if (cursor.getRow() != null) {
			editandoT = editando;
			cursorT = cursor;
			editorT = editor;
			tableT = table;
			editando.startEditing(cursor.getRow(), cursor.getColumn());
			editar(-1);
		}
	}

	@Override
	public void onCursorMouseDown(EditingTCE editando, TableCursor cursor,
			ControlEditor editor, Table table) {
		// System.out.println("cursor mouse down ");
		if (cursor.getRow() != null) {
			editandoT = editando;
			cursorT = cursor;
			editorT = editor;
			tableT = table;
			editando.startEditing(cursor.getRow(), cursor.getColumn());
			editar(-1);
		}
	}

	@Override
	public void onTableMouseDown(EditingTCE editando) {
		String txt = wgEditor.getText();
		editando.setText(txt);
		if (modifyListener!=null) modifyListener.modifyCell(tableT.getSelectionIndex(), (Object) wgEditor.getText());
		editando.endEditing();
		//wgEditor = null; //
		wgEditor.dispose();
	}
	
	private void editar(int seleccionar){
		//System.out.println("editanto CBO");
			//table.setSelection(edtRow);
			wgEditor = new Combo(cursorT, wgSWT);
			wgEditor.setItems(this.TXTList);
			if (selectionListener!=null) wgEditor.addSelectionListener(selectionListener);
			if (seleccionar==-1) {
				wgEditor.select(wgEditor.indexOf(TXTDefault));
				//wgEditor.setText(editandoT.getText());
			}
			else {
				wgEditor.select(seleccionar);
			}
			
			wgEditor.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent e) {
					//table.setSelection(cursor.getRow());
					TableItem row = editandoT.getRow();
					int column = editandoT.getColumn();
					int rowi = tableT.getSelectionIndex();
					//	 close the text editor and copy the data over
					//	 when the user hits "ENTER"
					if (e.character == SWT.CR) {
						row.setText(column, wgEditor.getText());
						if (modifyListener!=null) modifyListener.modifyCell(rowi, (Object) wgEditor.getText());
						wgEditor.dispose();
						editandoT.endEditing();
						cursorT.setFocus();
					}
//					close the text editor when the user hits "ESC"
					else if (e.character == SWT.ESC) {
						wgEditor.dispose();
						editandoT.endEditing();
						cursorT.setFocus();
					}
					else if(e.keyCode==SWT.ARROW_LEFT && (e.stateMask & SWT.CONTROL) != 0){
						row.setText(column, wgEditor.getText());
						if (modifyListener!=null) modifyListener.modifyCell(rowi, (Object) wgEditor.getText());
						if (column>1) cursorT.setSelection(rowi, column-1);
						wgEditor.dispose();
						editandoT.endEditing();
						cursorT.setFocus();
					}
					else if(e.keyCode==SWT.ARROW_RIGHT && (e.stateMask & SWT.CONTROL) != 0){
						row.setText(column, wgEditor.getText());
						if (modifyListener!=null) modifyListener.modifyCell(rowi, (Object) wgEditor.getText());
						if (column<tableT.getColumnCount()-1) cursorT.setSelection(rowi, column+1);
						wgEditor.dispose();
						editandoT.endEditing();
						cursorT.setFocus();
					}
					else if(e.keyCode==SWT.ARROW_DOWN && (e.stateMask & SWT.CONTROL) != 0){
						row.setText(column, wgEditor.getText());
						if (modifyListener!=null) modifyListener.modifyCell(rowi, (Object) wgEditor.getText());
						if (rowi<tableT.getItemCount()-1){
							cursorT.setSelection(rowi+1,column);
							tableT.setSelection(cursorT.getRow());
						}
						wgEditor.dispose();
						editandoT.endEditing();
						cursorT.setFocus();
					}
					else if(e.keyCode==SWT.ARROW_UP && (e.stateMask & SWT.CONTROL) != 0){
						row.setText(column, wgEditor.getText());
						if (modifyListener!=null) modifyListener.modifyCell(rowi, (Object) wgEditor.getText());
						if (rowi>1){
							cursorT.setSelection(rowi-1, column);
							tableT.setSelection(cursorT.getRow());
						}
						wgEditor.dispose();
						editandoT.endEditing();
						cursorT.setFocus();
					}
					//cursorT.setFocus();
				}
			});
			editorT.setEditor(wgEditor);
			wgEditor.setFocus();	
	}
	
	// funciones de listener
	public void addModifyCellListener(ModifyCellListener listener) {
		modifyListener = listener;
	}
	
	public void addSelectionListener(SelectionListener listener) {
		selectionListener =listener;
	}
	
	public void removeModifyCellListener() {
		modifyListener = null;
	}

}
